from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.Qt import Qt
from weather_chick import *
import sys
class Myweather():
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.window = QWidget()
        self.window.setGeometry(300,300,400,600)
        self.window.setWindowTitle('天气查询系统')
        self.window.setWindowIcon(QIcon('./lev4/small_enemy.png'))
        self.grid = QGridLayout()
        self.grid.setSpacing(10)
        self.window.setLayout(self.grid)
        self.lb_search = QLabel('天气查询')
        self.lb_search.setAlignment(Qt.AlignCenter)
        self.lb_search.setFont(QFont('Times', 21))
        self.grid.addWidget(self.lb_search,0,0,1,2)
        self.lb_search = QLabel('城市：')
        self.lb_search.setAlignment(Qt.AlignCenter)
        self.lb_search.setFont(QFont('Times', 16))
        self.grid.addWidget(self.lb_search,1,0,1,1)
        self.edit_city = QLineEdit()
        self.edit_city.setFont(QFont('Times', 16))
        self.grid.addWidget(self.edit_city,1,1,1,1)
        self.btn_1d = QPushButton('查看今日天气')
        self.btn_1d.setFont(QFont('Times', 13))
        self.grid.addWidget(self.btn_1d,2,0,1,1)
        self.btn_7d = QPushButton('查看未来7日天气')
        self.btn_7d.setFont(QFont('Times', 13))
        self.grid.addWidget(self.btn_7d,2,1,1,1)
        self.weather_info = QTextBrowser()
        self.weather_info.setText('这里用来展示天气信息')
        self.weather_info.setFont(QFont('Times', 13))
        self.grid.addWidget(self.weather_info,3,0,1,2)
    def set_1d_weather(self):
        text = self.edit_city.text()
        data = ''
        if text:
            ret = spider_1dweather(text)
            if ret:
                for key,val in ret['daytime'].items():
                    if val:
                        data += val + '\n'
                data += '\n'
                for key,val in ret['night'].items():
                    if val:
                        data += val + '\n'
                data += '\n'
            else:
                data = '没有查询到你想查询的城市的今日天气'
        else:
            data = '正在查询你想查询的城市的今日天气'
        self.weather_info.setText(data)
    def set_7d_weather(self):
        text = self.edit_city.text()
        data = ''
        if text:
            ret = spider_7dweather(text)
            if ret:
                for i in range(7):
                    for key, val in ret[str(i)].items():
                        if val:
                            data += val + '\n'
                    data += '\n'
            else:
                data = '没有查询到你想查询的城市的七日天气'
        else:
            data = '正在查询你想查询的城市的七日天气'
        self.weather_info.setText(data)
    def run(self):
        self.btn_1d.clicked.connect(lambda:self.set_1d_weather())
        self.btn_7d.clicked.connect(lambda: self.set_7d_weather())
        self.window.show()
        self.app.exec_()
if __name__ == '__main__':
    window = Myweather()
    window.run()